home *** CD-ROM | disk | FTP | other *** search
/ TPUG - Toronto PET Users Group / TPUG Users Group CD / TPUG Users Group CD.iso / AMIGA / AMICUS / AMICUS04.ADF / C / joystik.c < prev    next >
C/C++ Source or Header  |  1985-10-30  |  9KB  |  270 lines

  1. /* joystick demo v1.0 */
  2. /****************************************************************
  3. *                                                               *
  4. * Copyright 1985, Commodore Amiga Inc.  All rights reserved.    *
  5. * No part of this program may be reproduced, transmitted,       *
  6. * transcribed, stored in retrieval system, or translated into   *
  7. * any language or computer language, in any form or by any      *
  8. * means, electronic, mechanical, magnetic, optical, chemical,   *
  9. * manual or otherwise, without the prior written permission of  *
  10. * Commodore Amiga Incorporated, 983 University Ave, #D          *
  11. * Los Gatos, CA 95030                                           *
  12. *                                                               *
  13. ****************************************************************/
  14.  
  15.  
  16. /* *********************************************************************** */
  17. /* joystick test, for right game port on the Amiga.
  18.  
  19.    Notes:  The right port is used for this test because the input.device
  20.    task is busy continuously with the lefthand port, feeding input events
  21.    to intuition or console devices.  If Intuition is not activated
  22.    (applications which take over the whole machine may decide not to
  23.    activate Intuition), and if no console.device is activated either,
  24.    the input.device will never activate... allowing the application
  25.    free reign to use either the left OR the right hand joystick/mouse
  26.    port.  If either intuition or the console device are activated,
  27.    the lefthand port will yield, at best, every alternate input
  28.    event to an external application such as this test program.
  29.    This will undoubtedly mess up either of the two applications
  30.    and should therefore be avoided.  It was ok to use the right
  31.    port in this case, since the system has no particular interest
  32.    in monitoring it.
  33.  
  34.    Author:  Rob Peck  10/11/85
  35. *********************************************************************** */
  36.  
  37.  
  38. #include "intuall.h"
  39.  
  40. LONG GfxBase=0;
  41.  
  42. #define XMOVE 10
  43. #define YMOVE 10
  44. #define MAX(m,n) (m > n ? m : n)
  45.  
  46. struct InputEvent *game_data;   /* pointer into the returned data area
  47.                                  * where input event has been sent */
  48. SHORT           error;
  49.  
  50. struct IOStdReq *game_io_msg;
  51.  
  52. BYTE            gamebuffer[sizeof( struct InputEvent )];
  53. BYTE            *gamebuff;
  54.  
  55. SHORT           testval;
  56. SHORT           codevalue;
  57.         
  58. struct Port     *game_msg_port;
  59.  
  60. SHORT movesize;
  61. extern struct Port *CreatePort();
  62. extern struct IOStdReq *CreateStdIO();          
  63.  
  64. SHORT codeval, timeouts;
  65.  
  66. #define IF_NOT_IDLE_TWO_MINUTES while(timeouts < 4)
  67.  
  68. main()
  69. {
  70.         printf("Joystick Demo\n");
  71.         printf("\nPlug a Joystick Into Right Port\n");
  72.         printf("\nThen move the stick and click its buttons");
  73.  
  74.         
  75.         gamebuff = &gamebuffer[0];      
  76.                 /* point to first location in game buffer */
  77.  
  78. /* SYSTEM DEVICE COMMUNICATIONS SUPPORT SETUP ROUTINES *************** */
  79.  
  80.         game_msg_port = CreatePort(0,0);        
  81.                 /* provide a port for the IO response */
  82.         if(game_msg_port == 0)
  83.                 {
  84.                 printf("\nError While Performing CreatePort");
  85.                 exit(-1);
  86.                 }
  87.  
  88.         game_io_msg = CreateStdIO(game_msg_port);     
  89.                 /* make an io request block for communicating with
  90.                                 the gameport */
  91.  
  92.         if(game_io_msg == 0)
  93.                 {
  94.                 printf("\nError While Performing CreateStdIO");
  95.                 DeletePort(game_msg_port);
  96.                 exit(-2);
  97.                 }
  98. /* ********************************************************************** */
  99. /* OPEN THE DEVICE */
  100.  
  101.         error = OpenDevice("gameport.device",1,game_io_msg,0);
  102.                 /* open the device for access, unit 1 is right port */
  103.  
  104.         if(error != 0)
  105.                 {
  106.                 printf("\nError while opening the device, exiting");
  107.                 DeleteStdIO(game_io_msg);
  108.                 DeletePort(game_msg_port);
  109.                 exit(-3);
  110.                 }
  111. /* ********************************************************************** */
  112. /* SET THE DEVICE TYPE */
  113.  
  114.         game_data = (struct InputEvent *)gamebuffer;
  115.  
  116.                 /* test the joystick in this loop */
  117.         
  118.         if (set_controller_type(GPCT_ABSJOYSTICK) != 0)
  119.                 {
  120.                 printf("\nError while trying to set GPCT_ABSJOYSTICK");
  121.                 DeleteStdIO(game_io_msg);
  122.                 DeletePort(game_msg_port);
  123.                 exit(-4);
  124.                 }
  125. /* ********************************************************************** */
  126. /* SET THE DEVICE TRIGGER */
  127.         if (set_controller_trigger() != 0)
  128.                 {
  129.                 printf("\nError while trying to set controller trigger");
  130.                 DeleteStdIO(game_io_msg);
  131.                 DeletePort(game_msg_port);
  132.                 exit(-4);
  133.                 }
  134. /* ********************************************************************** */
  135. /* TELL USER WHAT YOU WILL BE DOING */
  136.  
  137.         printf("\nI will report: \n");
  138.         printf("\n     Stick X or Y moves");
  139.         printf("\n     Button presses (along with stick moves if any)");
  140.  
  141. /* ********************************************************************** */
  142. /* SETUP THE IO MESSAGE BLOCK FOR THE ACTUAL DATA READ */
  143.  
  144.         game_io_msg->io_Command = GPD_READEVENT;        
  145.                 /* from now on, just read input events */
  146.         game_io_msg->io_Data = gamebuffer;              
  147.                 /* into the input buffer, one at a time. */
  148.                 /* read-event waits for the preset conditions */
  149.         game_io_msg->io_Length = sizeof(struct InputEvent);     
  150.                 /* read one event each time we go back to the gameport */
  151.         game_io_msg->io_Data = &gamebuffer[0];  
  152.                 /* show where to put the data when read */
  153.         game_io_msg->io_Flags = 0;
  154.                 /* dont use quick io */
  155.  
  156. /* ********************************************************************** */
  157. /* LOOP FOREVER */
  158.  
  159.     FOREVER
  160.         {
  161.         game_io_msg->io_Length = sizeof(struct InputEvent);     
  162.                 /* read one event each time we go back to the gameport */
  163.  
  164.         printf("\n Waiting For Joystick Report\n");
  165.         SendIO(game_io_msg);
  166.         WaitPort(game_msg_port);
  167.                 /* this is NOT a busy wait... it is a task-sleep */
  168.         GetMsg(game_msg_port);
  169.  
  170.         codevalue = game_data->ie_Code;
  171.  
  172.         if(codevalue == IECODE_LBUTTON) 
  173.                 printf("\nFire Button pressed");
  174.         if(codevalue == (IECODE_LBUTTON + IECODE_UP_PREFIX))
  175.                 printf("\nFire Button released");
  176.  
  177.         which_direction();      
  178.         showbugs();
  179.         }
  180.  
  181. /* PROGRAM EXIT ..... temporarily no way to get here from FOREVER */ 
  182.         set_controller_type(GPCT_NOCONTROLLER);
  183.  
  184.         CloseDevice(game_io_msg);
  185.         DeleteStdIO(game_io_msg);
  186.         DeletePort(game_msg_port);
  187.         
  188.         printf("\nExiting program... 2 minutes with no activity sensed\n1> ");
  189.         return;
  190. }               
  191.  
  192. int which_direction()           
  193. {
  194.         SHORT xmove, ymove;
  195.         xmove = game_data->ie_X;
  196.         ymove = game_data->ie_Y;
  197.  
  198.         switch(ymove) 
  199.                 {
  200.                 case (-1):
  201.                         printf("\nForward");
  202.                         break;
  203.                 case (1):
  204.                         printf("\nBack");
  205.                         break;
  206.                 default:
  207.                         break;
  208.                 }
  209.         switch(xmove) 
  210.                 {
  211.                 case (-1):
  212.                         printf("\nLeft");
  213.                         break;
  214.                 case (1):
  215.                         printf("\nRight");
  216.                         break;
  217.                 default:
  218.                         break;
  219.                 }
  220.         return(0);
  221. }
  222.  
  223. int set_controller_type(type)
  224. SHORT type;
  225. {
  226.         game_io_msg->io_Command = GPD_SETCTYPE;   
  227.                 /* set type of controller to mouse */
  228.         game_io_msg->io_Length = 1;
  229.         game_io_msg->io_Data = gamebuff;
  230.         *gamebuff = type;       
  231.  
  232.         SendIO(game_io_msg);    
  233.                 /* set it up */
  234.                 /* this command doesn't wait... returns immediately */
  235.         WaitPort(game_msg_port);
  236.         GetMsg(game_msg_port);
  237.         return(game_io_msg->io_Error);
  238. }
  239.  
  240. int set_controller_trigger()
  241. {
  242.         struct GamePortTrigger gpt;
  243.  
  244.         game_io_msg->io_Command = GPD_SETTRIGGER;   
  245.         game_io_msg->io_Length = sizeof(gpt);
  246.         game_io_msg->io_Data = &gpt;
  247.         gpt.gpt_Keys = GPTF_UPKEYS+GPTF_DOWNKEYS;
  248.         gpt.gpt_Timeout = 0;
  249.         gpt.gpt_XDelta = 1;
  250.         gpt.gpt_YDelta = 1;
  251.  
  252.         return(DoIO(game_io_msg));
  253. }
  254.  
  255. showbugs()
  256. {
  257.         struct InputEvent *e;
  258.  
  259.         e = &gamebuffer[0];     /* where the input event gets placed */
  260.         printf("\nie_Class = %lx",e->ie_Class);
  261.         printf("\nie_SubClass = %lx",e->ie_SubClass);
  262.         printf("\nie_Code = %lx", e->ie_Code);
  263.         printf("\nie_Qualifier = %lx",e->ie_Qualifier);
  264.         printf("\nie_X = %ld", e->ie_X);
  265.         printf("\nie_Y = %ld", e->ie_Y);
  266.         printf("\nie_TimeStamp = %lx",e->ie_TimeStamp);
  267.         return;
  268. }       
  269.  
  270.